<?php


/**
 *
 * This function calculates ressources needed to build the building
 * @param $buildingName
 * @param $buildingTable
 * @param $townId
 */
function leftToBuild($buildingName, $buildingTable, $townId){

}

/**
 *
 * This is used to update the ressources
 * @param $townId
 */
function updateResourcesOfId($dbc, $townId){
	$goldMul = 1.2;
	$stoneMul = 1.23;
	$woodMul = 1.23;
	$metalMul = 1.1;

	$goldBegin = 30/3600;
	$stoneBegin = 50/3600;
	$woodBegin = 50/3600;
	$metalBegin = 0;

	$oldTime = mysqli_query($dbc, "SELECT last_activity FROM last_activity WHERE townid='$townId'");
	$oldTime = mysqli_fetch_row($oldTime);
	$oldTime = $oldTime[0];
	$oldTime = strtotime($oldTime);
	//how many sec have passed since last update

	$newDate = date("Y-m-d G:i:s"); //G for 24h format
	$unixNew = strtotime($newDate);
	$dif = $unixNew - $oldTime;
	// save actual resources
	$query = mysqli_query($dbc, "SELECT gold, stone, wood, metal FROM resources WHERE townid='$townId'");
	$query = mysqli_fetch_row($query);
	$gold = $query[0];
	$stone = $query[1];
	$wood = $query[2];
	$metal = $query[3];
	//write actual time to db
	mysqli_query($dbc, "UPDATE last_activity SET last_activity='$newDate' WHERE townid='$townId'");

	$resLevel = mysqli_query($dbc, "SELECT goldplant FROM resource_buildings WHERE townid='$townId'");
	$resLevel = mysqli_fetch_row($resLevel);
	$goldLevel = $resLevel[0];
	$resLevel = mysqli_query($dbc, "SELECT stoneplant FROM resource_buildings WHERE townid='$townId'");
	$resLevel = mysqli_fetch_row($resLevel);
	$stoneLevel = $resLevel[0];
	$resLevel = mysqli_query($dbc, "SELECT woodplant FROM resource_buildings WHERE townid='$townId'");
	$resLevel = mysqli_fetch_row($resLevel);
	$woodLevel = $resLevel[0];
	$resLevel = mysqli_query($dbc, "SELECT metalplant FROM resource_buildings WHERE townid='$townId'");
	$resLevel = mysqli_fetch_row($resLevel);
	$metalLevel = $resLevel[0];
	//calculate how many resources was produced
	for ($i=0; $i<$goldLevel; $i++){
		$goldBegin = $goldBegin * $goldMul;
	}
	for ($i=0; $i<$stoneLevel; $i++){
		$stoneBegin = $stoneBegin * $stoneMul;
	}
	for ($i=0; $i<$woodLevel; $i++){
		$woodBegin = $woodBegin * $woodMul;
	}
	if($metalLevel>0){
		$metalBegin = 25/3600;
	}
	for ($i=0; $i<$metalLevel; $i++){
		$metalBegin = $metalBegin * $metalMul;
	}
	//new sum of resources
	$actualGold=$goldBegin*$dif+$gold;
	$actualWood=$woodBegin*$dif+$wood;
	$actualStone=$stoneBegin*$dif+$stone;
	$actualMetal=$metalBegin*$dif+$metal;
	$max=testLagerCapacity($dbc, $townId);
	if($max<=$actualGold){
		$actualGold = $max;
	}
	if($max<=$actualStone){
		$actualStone=$max;
	}
	if($max<=$actualWood){
		$actualWood=$max;
	}
	if($max<=$actualMetal){
		$actualMetal=$max;
	}
	mysqli_query($dbc, "UPDATE resources SET gold='$actualGold', wood='$actualWood', stone='$actualStone', metal='$actualMetal' WHERE townid=$townId ");
}//updateResourceOfId

//UPDATE resources SET gold=400, wood=400, stone=400, metal=0 WHERE townid=1;


function testLagerCapacity($dbc, $townId){
	$query = mysqli_query($dbc, "SELECT lager FROM resource_buildings WHERE townid='$townId'");
	$query = mysqli_fetch_row($query);
	$lagerLevel = $query[0];
	$resMax = 700;
	for($i=0; $i<$lagerLevel; $i++){
		$resMax = $resMax + 700;
	}
	return $resMax;
}

function findWhatIsBuilding($dbc, $tablename){
	global $townId;
	$query = mysqli_query($dbc, "SELECT bname FROM $tablename WHERE townid='$townId'");
	$query = mysqli_fetch_row($query);
	$bname = $query[0];
	return $bname;
}


function countDown($test){

	$build_start;
	$build_end;
	$timeToPrint = <<<EOT
	<script language="JavaScript">
	int build_start, build_end;
	startTime = new Date(); //aktuelles Datum
	
	function countdown(){
	if(startTime<build_end){
		int hours, minutes, seconds;
		
		countdownTime = build_end - startTime;
		
		if(countdownTime/3600!=0){
		hours=countdownTime/3600;
		countdownTime=countdownTime - hours * 3600;
		} else {
		hours = 0;
		}
		if(countdownTime/60!=0){
		minutes=countdownTime/60;
		countdownTime = countdownTime - minutes * 60;
		} else {
		minutes = 0;
		}
		seconds = countdownTime;
		
		//formatting
		(hours!=1)?hours=hours+" часа,  ":hours=hours+" часа,  ";
          (minutes!=1)?minutes=minutes+" минут и ":minutes=minutes+" минут  и  ";
          if(seconds<10) seconds="0"+seconds;
          (seconds!=1)?seconds=seconds+" секунд":seconds=seconds+" секунд";

          document.countdownform.countdowninput.value=
              hours+minutes+seconds;

          setTimeout('countdown()',200);
	} else document.countdownform.countdowninput.value=
            "построено";
     }
	</script>
	
	<body onload="countdown()">
    <form name="countdownform">
      <p>
        <input size="75" name="countdowninput">
      </p>
    </form>
  </body>
	
	
EOT;

}
	
	
	